package com.changhai.diabetes.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.changhai.diabetes.model.LabBlood;
import com.changhai.diabetes.model.LabGanshen;
import com.changhai.diabetes.model.LabLipids;
import com.changhai.diabetes.model.LabOther;
import com.changhai.diabetes.model.LabPancreatCtai;
import com.changhai.diabetes.model.LabPancreatOgtt;
import com.changhai.diabetes.model.LabPancreatYidaosu;
import com.changhai.diabetes.model.LabPhysical;
import com.changhai.diabetes.model.LabSugar;
import com.changhai.diabetes.model.LabUrine;
import com.changhai.diabetes.service.LabService;
import com.changhai.diabetes.util.Constants;

/**
 * 实验室
 * 
 * @author Andy
 * 
 */
public class LabServiceImpl extends CommonServiceImpl implements LabService {
	public LabPhysical searchPhyByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPhysical.getByPatientId", patientid);
		if (o != null && o instanceof LabPhysical) {
			return (LabPhysical) o;
		} else {
			LabPhysical p = new LabPhysical();
			p.setPatientid(patientid);
			return p;
		}
	}

	public LabSugar searchSugarByPatientId(Long patientid) {
		List<LabSugar> list = new ArrayList<LabSugar>();
		list = this.getList(Constants.DS_CHANGHAI, "labSugar.getByPatientId", patientid);
		if (list != null && list.size() > 0) {
			return (LabSugar) list.get(0);
		} else {
			LabSugar o = new LabSugar();
			o.setPatientid(patientid);
			o.setTestmethod(1);
			o.setTesttime(1);
			return o;
		}
	}

	public LabBlood searchBloodByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labBlood.getByPatientId", patientid);
		if (o != null && o instanceof LabBlood) {
			return (LabBlood) o;
		} else {
			LabBlood b = new LabBlood();
			b.setPatientid(patientid);
			return b;
		}
	}

	public LabGanshen searchGanshenByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labGanshen.getByPatientId", patientid);
		if (o != null && o instanceof LabGanshen) {
			return (LabGanshen) o;
		} else {
			LabGanshen b = new LabGanshen();
			b.setPatientid(patientid);
			return b;
		}
	}

	public LabLipids searchLipidsByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labLipids.getByPatientId", patientid);
		if (o != null && o instanceof LabLipids) {
			return (LabLipids) o;
		} else {
			LabLipids b = new LabLipids();
			b.setPatientid(patientid);
			return b;
		}
	}

	public LabOther searchOtherByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labOther.getByPatientId", patientid);
		if (o != null && o instanceof LabOther) {
			return (LabOther) o;
		} else {
			LabOther b = new LabOther();
			b.setPatientid(patientid);
			return b;
		}
	}

	public LabUrine searchUrineByPatientId(Long patientid) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labUrine.getByPatientId", patientid);
		if (o != null && o instanceof LabUrine) {
			return (LabUrine) o;
		} else {
			LabUrine b = new LabUrine();
			b.setPatientid(patientid);
			return b;
		}
	}

	public void saveEditLabPhy(LabPhysical labphy) {
		if (labphy != null) {
			if (labphy.getPhysicalid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labPhysical.updateByPrimaryKey", labphy);
			} else {
				Long phyid = this.saveNewLabPhy(labphy);
				if (phyid != null && phyid > 0) {
					labphy.setPhysicalid(phyid);
					this.updateObject(Constants.DS_CHANGHAI, "labPhysical.updateByPrimaryKey", labphy);
				}
			}
		}
	}

	public void saveEditLabSugar(LabSugar labsugar) {
		if (labsugar != null) {
			if (labsugar.getSugarid() != null) {
				if (searchIfExits(labsugar)) {
					this.updateObject(Constants.DS_CHANGHAI, "labSugar.updateByPrimaryKey", labsugar);
				} else {
					labsugar.setSugarid(null);
					Long id = this.saveNewLabSugar(labsugar);
					if (id != null && id > 0) {
						labsugar.setSugarid(id);
					}
				}
			} else {
				Long sugarid = this.saveNewLabSugar(labsugar);
				if (sugarid != null && sugarid > 0) {
					labsugar.setSugarid(sugarid);
					this.updateObject(Constants.DS_CHANGHAI, "labSugar.updateByPrimaryKey", labsugar);
				}
			}
		}
	}

	public void saveEditLabBlood(LabBlood labblood) {
		if (labblood != null) {
			if (labblood.getBloodid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labBlood.updateByPrimaryKey", labblood);
			} else {
				Long bloodid = this.saveNewLabBlood(labblood);
				if (bloodid != null && bloodid > 0) {
					labblood.setBloodid(bloodid);
					this.updateObject(Constants.DS_CHANGHAI, "labBlood.updateByPrimaryKey", labblood);
				}
			}
		}
	}

	public void saveEditLabGanshen(LabGanshen labganshen) {
		if (labganshen != null) {
			if (labganshen.getGanshenid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labGanshen.updateByPrimaryKey", labganshen);
			} else {
				Long ganshenid = this.saveNewLabGanshen(labganshen);
				if (ganshenid != null && ganshenid > 0) {
					labganshen.setGanshenid(ganshenid);
					this.updateObject(Constants.DS_CHANGHAI, "labGanshen.updateByPrimaryKey", labganshen);
				}
			}
		}
	}

	public void saveEditLabLipids(LabLipids lablipids) {
		if (lablipids != null) {
			if (lablipids.getLipidsid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labLipids.updateByPrimaryKey", lablipids);
			} else {
				Long lipidsid = this.saveNewLabLipids(lablipids);
				if (lipidsid != null && lipidsid > 0) {
					lablipids.setLipidsid(lipidsid);
					this.updateObject(Constants.DS_CHANGHAI, "labLipids.updateByPrimaryKey", lablipids);
				}
			}
		}
	}

	public void saveEditLabOther(LabOther labOther) {
		if (labOther != null) {
			if (labOther.getOtherid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labOther.updateByPrimaryKey", labOther);
			} else {
				Long otherid = this.saveNewLabOther(labOther);
				if (otherid != null && otherid > 0) {
					labOther.setOtherid(otherid);
					this.updateObject(Constants.DS_CHANGHAI, "labOther.updateByPrimaryKey", labOther);
				}
			}
		}
	}

	public void saveEditLabUrine(LabUrine labUrine) {
		if (labUrine != null) {
			if (labUrine.getUrineid() != null) {
				this.updateObject(Constants.DS_CHANGHAI, "labUrine.updateByPrimaryKey", labUrine);
			} else {
				Long urineid = this.saveNewLabUrine(labUrine);
				if (urineid != null && urineid > 0) {
					labUrine.setUrineid(urineid);
					this.updateObject(Constants.DS_CHANGHAI, "labUrine.updateByPrimaryKey", labUrine);
				}
			}
		}
	}

	public Long saveNewLabPhy(LabPhysical labphy) {
		Integer id = 0;
		if (labphy != null && labphy.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labPhysical.insert", labphy);
		}
		return new Long(id);
	}

	public Long saveNewLabSugar(LabSugar labsugar) {
		Integer id = 0;
		if (labsugar != null && labsugar.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labSugar.insert", labsugar);
		}
		return new Long(id);
	}

	public Long saveNewLabBlood(LabBlood labblood) {
		Integer id = 0;
		if (labblood != null && labblood.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labBlood.insert", labblood);
		}
		return new Long(id);
	}

	public Long saveNewLabGanshen(LabGanshen labganshen) {
		Integer id = 0;
		if (labganshen != null && labganshen.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labGanshen.insert", labganshen);
		}
		return new Long(id);
	}

	public Long saveNewLabLipids(LabLipids lablipids) {
		Integer id = 0;
		if (lablipids != null && lablipids.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labLipids.insert", lablipids);
		}
		return new Long(id);
	}

	public Long saveNewLabOther(LabOther labother) {
		Integer id = 0;
		if (labother != null && labother.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labOther.insert", labother);
		}
		return new Long(id);
	}

	public Long saveNewLabUrine(LabUrine laburine) {
		Integer id = 0;
		if (laburine != null && laburine.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labUrine.insert", laburine);
		}
		return new Long(id);
	}

	public LabPancreatOgtt searchOgttByPatientId(Long patientid) {
		List<LabPancreatOgtt> list = new ArrayList<LabPancreatOgtt>();
		list = this.getList(Constants.DS_CHANGHAI, "labPancreatOgtt.getByPatientId", patientid);
		if (list != null && list.size() > 0) {
			return (LabPancreatOgtt) list.get(0);
		} else {
			LabPancreatOgtt o = new LabPancreatOgtt();
			o.setPatientid(patientid);
			o.setXtlevel(1);
			return o;
		}
	}

	public void saveEditLabOgtt(LabPancreatOgtt labogtt) {
		if (labogtt != null) {
			if (labogtt.getSeqid() != null) {
				if (searchByAmount(labogtt)) {
					this.updateObject(Constants.DS_CHANGHAI, "labPancreatOgtt.updateByPrimaryKey", labogtt);
				} else {
					labogtt.setSeqid(null);
					Long id = this.saveNewLabOgtt(labogtt);
					if (id != null && id > 0) {
						labogtt.setSeqid(id);
					}
				}
			} else {
				Long id = this.saveNewLabOgtt(labogtt);
				if (id != null && id > 0) {
					labogtt.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "labPancreatOgtt.updateByPrimaryKey", labogtt);
				}
			}
		}
	}

	public Long saveNewLabOgtt(LabPancreatOgtt labogtt) {
		Integer id = 0;
		if (labogtt != null && labogtt.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labPancreatOgtt.insert", labogtt);
		}
		return new Long(id);
	}

	private Boolean searchByAmount(LabPancreatOgtt labogtt) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("patientid", labogtt.getPatientid());
		map.put("xtlevel", labogtt.getXtlevel());
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPancreatOgtt.getByAmountAndPatientId", map);
		if (o != null && o instanceof LabPancreatOgtt) {
			return true;
		} else {
			return false;
		}
	}

	public LabPancreatYidaosu searchYidaosuByPatientId(Long patientid) {
		List<LabPancreatYidaosu> list = new ArrayList<LabPancreatYidaosu>();
		list = this.getList(Constants.DS_CHANGHAI, "labPancreatYidaosu.getByPatientId", patientid);
		if (list != null && list.size() > 0) {
			return (LabPancreatYidaosu) list.get(0);
		} else {
			LabPancreatYidaosu o = new LabPancreatYidaosu();
			o.setPatientid(patientid);
			o.setYdlevel(1);
			return o;
		}
	}

	public void saveEditLabYidaosu(LabPancreatYidaosu labyidaosu) {
		if (labyidaosu != null) {
			if (labyidaosu.getSeqid() != null) {
				if (searchYidaosuByAmount(labyidaosu)) {
					this.updateObject(Constants.DS_CHANGHAI, "labPancreatYidaosu.updateByPrimaryKey", labyidaosu);
				} else {
					labyidaosu.setSeqid(null);
					Long id = this.saveNewLabYidaosu(labyidaosu);
					if (id != null && id > 0) {
						labyidaosu.setSeqid(id);
					}
				}
			} else {
				Long id = this.saveNewLabYidaosu(labyidaosu);
				if (id != null && id > 0) {
					labyidaosu.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "labPancreatYidaosu.updateByPrimaryKey", labyidaosu);
				}
			}
		}
	}

	public Long saveNewLabYidaosu(LabPancreatYidaosu labyidaosu) {
		Integer id = 0;
		if (labyidaosu != null && labyidaosu.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labPancreatYidaosu.insert", labyidaosu);
		}
		return new Long(id);
	}

	private Boolean searchYidaosuByAmount(LabPancreatYidaosu labyidaosu) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("patientid", labyidaosu.getPatientid());
		map.put("ydlevel", labyidaosu.getYdlevel());
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPancreatYidaosu.getByAmountAndPatientId", map);
		if (o != null && o instanceof LabPancreatYidaosu) {
			return true;
		} else {
			return false;
		}
	}
	
	public LabPancreatCtai searchCtaiByPatientId(Long patientid) {
		List<LabPancreatCtai> list = new ArrayList<LabPancreatCtai>();
		list = this.getList(Constants.DS_CHANGHAI, "labPancreatCtai.getByPatientId", patientid);
		if (list != null && list.size() > 0) {
			return (LabPancreatCtai) list.get(0);
		} else {
			LabPancreatCtai o = new LabPancreatCtai();
			o.setPatientid(patientid);
			o.setCtlevel(1);
			return o;
		}
	}

	public void saveEditLabCtai(LabPancreatCtai labctai) {
		if (labctai != null) {
			if (labctai.getSeqid() != null) {
				if (searchCtaiByAmount(labctai)) {
					this.updateObject(Constants.DS_CHANGHAI, "labPancreatCtai.updateByPrimaryKey", labctai);
				} else {
					labctai.setSeqid(null);
					Long id = this.saveNewLabCtai(labctai);
					if (id != null && id > 0) {
						labctai.setSeqid(id);
					}
				}
			} else {
				Long id = this.saveNewLabCtai(labctai);
				if (id != null && id > 0) {
					labctai.setSeqid(id);
					this.updateObject(Constants.DS_CHANGHAI, "labPancreatCtai.updateByPrimaryKey", labctai);
				}
			}
		}
	}

	public Long saveNewLabCtai(LabPancreatCtai labctai) {
		Integer id = 0;
		if (labctai != null && labctai.getPatientid() != null) {
			id = this.insertObjectID(Constants.DS_CHANGHAI, "labPancreatCtai.insert", labctai);
		}
		return new Long(id);
	}

	private Boolean searchCtaiByAmount(LabPancreatCtai labctai) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("patientid", labctai.getPatientid());
		map.put("ctlevel", labctai.getCtlevel());
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPancreatCtai.getByAmountAndPatientId", map);
		if (o != null && o instanceof LabPancreatCtai) {
			return true;
		} else {
			return false;
		}
	}
	private Boolean searchIfExits(LabSugar labsugar) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("patientid", labsugar.getPatientid());
		map.put("testtime", labsugar.getTesttime());
		map.put("testmethod", labsugar.getTestmethod());
		Object o = this.getObject(Constants.DS_CHANGHAI, "labSugar.getByTimeAndType", map);
		if (o != null && o instanceof LabSugar) {
			return true;
		} else {
			return false;
		}
	}

	public LabPancreatOgtt searchOgttByPatientIdAndType(Map<String,Object> map) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPancreatOgtt.getByAmountAndPatientId", map);
		if (o != null && o instanceof LabPancreatOgtt) {
			return (LabPancreatOgtt) o;
		} else {
			LabPancreatOgtt p = new LabPancreatOgtt();
			p.setPatientid((Long) map.get("patientid"));
			return p;
		}
	}

	public LabPancreatYidaosu searchYidaosuByPatientIdAndType(Map<String,Object> map) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPancreatYidaosu.getByAmountAndPatientId", map);
		if (o != null && o instanceof LabPancreatYidaosu) {
			return (LabPancreatYidaosu) o;
		} else {
			LabPancreatYidaosu p = new LabPancreatYidaosu();
			p.setPatientid((Long) map.get("patientid"));
			return p;
		}
	}
	
	public LabPancreatCtai searchCtaiByPatientIdAndType(Map<String,Object> map) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labPancreatCtai.getByAmountAndPatientId", map);
		if (o != null && o instanceof LabPancreatCtai) {
			return (LabPancreatCtai) o;
		} else {
			LabPancreatCtai p = new LabPancreatCtai();
			p.setPatientid((Long) map.get("patientid"));
			return p;
		}
	}
	
	public LabSugar searchSugarByTimeAndType(Map<String,Object> map) {
		Object o = this.getObject(Constants.DS_CHANGHAI, "labSugar.getByTimeAndType", map);
		if (o != null && o instanceof LabSugar) {
			return (LabSugar) o;
		} else {
			LabSugar s = new LabSugar();
			s.setPatientid((Long) map.get("patientid"));
			return s;
		}
	}
}
